package com.zenithmind.task.controller;

import com.zenithmind.common.context.UserContext;
import com.zenithmind.common.result.PageResult;
import com.zenithmind.common.result.Result;
import com.zenithmind.task.pojo.dto.TaskCommentDTO;
import com.zenithmind.task.pojo.entity.TaskComment;
import com.zenithmind.task.pojo.query.TaskCommentQuery;
import com.zenithmind.task.pojo.vo.TaskCommentVO;
import com.zenithmind.task.service.TaskCommentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 任务评论控制器
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Slf4j
@RestController
@RequestMapping("/api/zenithMind/task/comment")
@RequiredArgsConstructor
@Tag(name = "任务评论管理", description = "任务评论的添加、回复、删除等功能")
public class TaskCommentController {

    private final TaskCommentService taskCommentService;

    @GetMapping("/page")
    @Operation(summary = "分页查询评论列表", description = "分页查询指定任务的评论列表")
    public Result<PageResult<TaskCommentVO>> getCommentPage(TaskCommentQuery query) {
        String userId = UserContext.requireUserId();
        PageResult<TaskCommentVO> result = taskCommentService.getCommentPage(query, userId);
        return Result.success(result);
    }

    @GetMapping("/tree")
    @Operation(summary = "查询评论树", description = "获取任务评论的树形结构（包含回复）")
    public Result<List<TaskCommentVO>> getCommentTree(
            @Parameter(description = "任务ID") @RequestParam String taskId) {
        String userId = UserContext.requireUserId();
        List<TaskCommentVO> result = taskCommentService.getCommentTree(taskId, userId);
        return Result.success(result);
    }

    @GetMapping("/{id}")
    @Operation(summary = "查询评论详情", description = "根据ID查询评论的详细信息")
    public Result<TaskCommentVO> getCommentById(@Parameter(description = "评论ID") @PathVariable String id) {
        String userId = UserContext.requireUserId();
        TaskCommentVO comment = taskCommentService.getCommentById(id, userId);
        return Result.success(comment);
    }

    @PostMapping
    @Operation(summary = "添加评论", description = "为任务添加新评论")
    public Result<Boolean> addComment(@Valid @RequestBody TaskCommentDTO commentDTO) {
        String userId = UserContext.requireUserId();
        TaskComment comment = new TaskComment();
        // TODO: 使用 BeanCopyUtils 复制属性
        comment.setTaskId(commentDTO.getTaskId());
        comment.setContent(commentDTO.getContent());
        comment.setCommentType(commentDTO.getCommentType());
        comment.setParentId(commentDTO.getParentId());
        comment.setReplyToId(commentDTO.getReplyToId());
        
        Boolean result = taskCommentService.addComment(comment, userId);
        return Result.success(result);
    }

    @PostMapping("/reply")
    @Operation(summary = "回复评论", description = "回复指定的评论")
    public Result<Boolean> replyComment(
            @Parameter(description = "父评论ID") @RequestParam String parentId,
            @Parameter(description = "回复内容") @RequestParam String content,
            @Parameter(description = "被回复人ID") @RequestParam(required = false) String replyToId) {
        String userId = UserContext.requireUserId();
        Boolean result = taskCommentService.replyComment(parentId, content, replyToId, userId);
        return Result.success(result);
    }

    @PutMapping
    @Operation(summary = "更新评论", description = "更新评论内容")
    public Result<Boolean> updateComment(@Valid @RequestBody TaskComment comment) {
        String userId = UserContext.requireUserId();
        Boolean result = taskCommentService.updateComment(comment, userId);
        return Result.success(result);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除评论", description = "删除指定的评论")
    public Result<Boolean> deleteComment(@Parameter(description = "评论ID") @PathVariable String id) {
        String userId = UserContext.requireUserId();
        Boolean result = taskCommentService.deleteComment(id, userId);
        return Result.success(result);
    }

    @PostMapping("/{id}/like")
    @Operation(summary = "点赞评论", description = "为评论点赞")
    public Result<Boolean> likeComment(@Parameter(description = "评论ID") @PathVariable String id) {
        String userId = UserContext.requireUserId();
        Boolean result = taskCommentService.likeComment(id, userId);
        return Result.success(result);
    }

    @DeleteMapping("/{id}/like")
    @Operation(summary = "取消点赞评论", description = "取消评论的点赞")
    public Result<Boolean> unlikeComment(@Parameter(description = "评论ID") @PathVariable String id) {
        String userId = UserContext.requireUserId();
        Boolean result = taskCommentService.unlikeComment(id, userId);
        return Result.success(result);
    }

    @PutMapping("/{id}/pin")
    @Operation(summary = "置顶评论", description = "将评论置顶")
    public Result<Boolean> pinComment(@Parameter(description = "评论ID") @PathVariable String id) {
        String userId = UserContext.requireUserId();
        Boolean result = taskCommentService.pinComment(id, userId);
        return Result.success(result);
    }

    @DeleteMapping("/{id}/pin")
    @Operation(summary = "取消置顶评论", description = "取消评论的置顶")
    public Result<Boolean> unpinComment(@Parameter(description = "评论ID") @PathVariable String id) {
        String userId = UserContext.requireUserId();
        Boolean result = taskCommentService.unpinComment(id, userId);
        return Result.success(result);
    }

    @GetMapping("/latest")
    @Operation(summary = "查询最新评论", description = "查询任务的最新评论")
    public Result<List<TaskCommentVO>> getLatestComments(
            @Parameter(description = "任务ID") @RequestParam String taskId,
            @Parameter(description = "数量限制") @RequestParam(defaultValue = "5") Integer limit) {
        String userId = UserContext.requireUserId();
        List<TaskCommentVO> result = taskCommentService.getLatestComments(taskId, limit, userId);
        return Result.success(result);
    }

    @GetMapping("/user")
    @Operation(summary = "查询用户评论", description = "查询用户的评论列表")
    public Result<PageResult<TaskCommentVO>> getUserComments(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "页大小") @RequestParam(defaultValue = "10") Integer pageSize) {
        String userId = UserContext.requireUserId();
        PageResult<TaskCommentVO> result = taskCommentService.getUserComments(userId, pageNum, pageSize);
        return Result.success(result);
    }
}
